package pl.prv.polanie.client.panels.lists;

import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import pl.prv.polanie.client.Planner;
import pl.prv.polanie.client.dialogs.CreateDeclarationDialog;
import pl.prv.polanie.client.dialogs.CreateSubjectEventDialog;
import pl.prv.polanie.client.dto.CSDeclarationDTO;
import pl.prv.polanie.client.dto.CSSubgroupDTO;
import pl.prv.polanie.client.dto.CSSubjectEventDTO;
import pl.prv.polanie.client.dto.CSVDeclarationDTO;
import pl.prv.polanie.client.dto.ICSDTO;
import pl.prv.polanie.client.model.Cache;
import pl.prv.polanie.client.model.ViewDataProvider;
import pl.prv.polanie.client.widgets.schedule.Schedule;
import pl.prv.polanie.client.widgets.table.TableDeclarations;

import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Widget;

public class DeclarationsList extends ObjectsList {

	TableDeclarations tableDeclarations = null;
	Button addSubjectEventsBasingOnSelectedDeclaration = new Button("Add subject event basing on selected declaration");

	
	final Schedule schedule;
	
	/**
	 * Zwraca index w kolekcji declarations elementu odpowiadajacego podanemu csSE,
	 * czyli posiadajacemu te same wartosci:
	 * 	subjectID,
	 * 	subgroupTypeID,
	 * 	duration
	 * 
	 * Jesli nie ma w kolekcji takiego elmentu, zwraca -1.
	 * 
	 * @param declarations
	 * @param csSE
	 * @return
	 */
	private int arrayOfDeclarationsContainsSE(CSDeclarationDTO[] declarations, CSSubjectEventDTO csSE) {
		
		for (int i = 0; i < declarations.length; i++) {
			CSDeclarationDTO csD = declarations[i];
			
			CSSubgroupDTO subgroup = null;
			if (csSE.getSubgroupID() != -1) {
				subgroup = Cache.getCSSubgroupDTOByID(csSE.getSubgroupID());	
			}
			
			//w se jest ustawiona subgroup
			if (subgroup != null) {
				
				if (csD.getSubjectID() == csSE.getSubjectID()
						&& csD.getSubgroupTypeID() == subgroup.getSubgroupTypeID()
//						&& csD.getDuration().equals(csSE.getDuration())
				) {
					Planner
							.log("DeclarationsList.arrayOfDeclarationsContainsSE: "
									+ "ZNALAZLEM ODPOWIEDNIA DECLARATION POD INDEKSEM " + i);
					return i;
				}
			}
			
		}
		
		return -1;
	}
	
	
	public DeclarationsList(Schedule schedule, final long groupID) {
		
		tableDeclarations = new TableDeclarations(groupID, this);
		this.schedule = schedule; 
		
		CSDeclarationDTO[] ds;
		
		if (groupID != -1) {
			ds = Cache.getDeclarationsByGroupID(groupID);
		} else {
			ds = Cache.getAllDeclarations();
		}
		
		for (int i = 0; i < ds.length; i++) {
//			ds[i].setToSchedule(new Integer(ds[i].getSubgroupsAmount().intValue()));
			int subgorupsCount = Cache.getSubgroupsCountByGroupIDAndSubgroupTypeID(groupID, ds[i].getSubgroupTypeID());
			int toSchedule = subgorupsCount * ds[i].getDuration().intValue()/45;
			ds[i].setToSchedule(new Integer(toSchedule));
		}
		
		
		Planner.log("DeclarationsList.DeclarationsList: " + "  --------------- > ZACZYNAMY ");
		Planner.log("DeclarationsList.DeclarationsList: " + "  --------------- > mamy declarations: " + 2);
		
		Map<Long, ICSDTO> seMap = schedule.getSubjectEventsList().getTableSubjectEvents().getCSSubjectEventDTOs();
		Set<Long> s  = seMap.keySet();
		Planner.log("DeclarationsList.DeclarationsList: " + "  --------------- > mamy subjectEvent: " + s.size());
		
		for (Iterator<Long> iter = s.iterator(); iter.hasNext();) {
			
			CSSubjectEventDTO se = (CSSubjectEventDTO)seMap.get(iter.next());
			int index = arrayOfDeclarationsContainsSE(ds, se);
			if (index != -1) {
				
				CSDeclarationDTO decl = ds[index];
				decl.setToSchedule(new Integer(decl.getToSchedule()-se.getDuration().intValue()/45));
			}
			
		}
		
		//pobrac z schedule wszystkie subjectEventy
		//pogropowac po subjectID, subgroupTypeID, duration + ile razy
		//od declaration jesli sie zgadza jakas grupa SE,
		//				to odjac i policzyc w ten sposob alreadyScheduled
		
		for (int i = 0; i < ds.length; i++) {
			
			CSDeclarationDTO cs = ds[i];
			CSVDeclarationDTO csv = ViewDataProvider.getCSVDeclaration(cs);
			
			tableDeclarations.addRow(cs, csv);
		}

		
		
		add(tableDeclarations);
		
		
		addRecordButton.addClickListener(new ClickListener(){
			public void onClick(Widget sender) {
//				Schedule schedule = tableDeclarations.getParentSchedule();
				
				CreateDeclarationDialog cdd = new CreateDeclarationDialog(groupID, DeclarationsList.this, null);
				cdd.center();
			}
		});
		
		removeSelectedRecordButton.addClickListener(new ClickListener() {
			public void onClick(Widget sender) {
				tableDeclarations.removeSelectedDeclarationFromTable();
			}
		});
		
		addSubjectEventsBasingOnSelectedDeclaration.addClickListener(new ClickListener() {
			public void onClick(Widget sender) {
				CreateSubjectEventDialog csed = new CreateSubjectEventDialog(DeclarationsList.this.schedule, tableDeclarations.getSelectedCSDto());
				csed.center();
			}
		});
		
		setupButtons();
		add(addSubjectEventsBasingOnSelectedDeclaration);
		addSubjectEventsBasingOnSelectedDeclaration.setEnabled(false);
	}

	
	public void addCSDeclaration(CSDeclarationDTO csDeclaration) {
//		CSVSubjectEventDTO csv = ViewDataProvider.getCSVSubjectEvent(csSubjectEvent);
		
		tableDeclarations.saveOrUpdateDeclarationInTable(csDeclaration);
//		tableSubjectEvents.addRow(csSubjectEvent, csv);
	}
	
	public void removeSelectdCSSubjectEvent() {
		tableDeclarations.removeSelectedDeclarationFromTable();
	}

	
	@Override
	public void showRemoveSelectedRecordButton() {
		super.showRemoveSelectedRecordButton();
		addSubjectEventsBasingOnSelectedDeclaration.setEnabled(true);
	}
	
	@Override
	public void hideRemoveSelectedRecordButton() {
		super.hideRemoveSelectedRecordButton();
		addSubjectEventsBasingOnSelectedDeclaration.setEnabled(false);
	}

	public void showAddSubjectEventsBasingOnSelectedDeclaration() {
		add(addSubjectEventsBasingOnSelectedDeclaration);
	}

	


}
